package com.example.flowable.car;

import com.example.FlowableDb;
import com.example.vo.process.EvectionVo;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.history.HistoricProcessInstanceQuery;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static java.lang.System.out;

/**
 * @author wsl
 */
public class FlowableHisotry extends FlowableDb {

    public FlowableHisotry(){
        super("car:1:d532aff5-e173-11ec-a19f-ac74b110b28d");
    }

    @Test
    public void queryProcessHistory() {
        HistoricProcessInstanceQuery historicProcessInstanceQuery = historyService
                .createHistoricProcessInstanceQuery();
        //查询用户启动的流程
        HistoricProcessInstance processInstance = historicProcessInstanceQuery
                .processInstanceId("78e57f55-df40-11ec-8bc0-00ff37603673")
                .singleResult();
        out.println("流程编号：" + processInstance.getId());
        out.println("结束时间：" + processInstance.getEndTime());

        List<HistoricActivityInstance> list = historyService
                .createHistoricActivityInstanceQuery()
                .processInstanceId(processInstance.getId())
                .activityType("userTask")
                .list();
        out.println("\n>>>>>>>流程实例节点记录");
        for (HistoricActivityInstance historicActivityInstance : list) {
            out.println("节点名称：" + historicActivityInstance.getActivityName());
            out.println("处理人：" + historicActivityInstance.getAssignee());
            out.println("处理毫秒数：" + historicActivityInstance.getDurationInMillis());
        }

        List<HistoricTaskInstance> list1 = historyService
                .createHistoricTaskInstanceQuery()
                .processInstanceId(processInstance.getId())
//                .includeProcessVariables()
//                .includeCaseVariables()
                .includeTaskLocalVariables()
                .list();
        out.println("\n>>>>>>>流程执行任务");
        for (HistoricTaskInstance historicTaskInstance : list1) {
            out.println("任务编号：" + historicTaskInstance.getId());
            out.println("任务名称：" + historicTaskInstance.getName());
//            System.out.println("任务参数：" + historicTaskInstance.getProcessVariables());
            out.println("任务参数：" + historicTaskInstance.getCaseVariables());
            out.println("任务参数：" + historicTaskInstance.getTaskLocalVariables());
        }

//        historyService
//                .create
    }

    /**
     * 查看我发起的出差流程
     */
    @Test
    public void queryMyStartEvection() {
        //1、查询我发起的出差流程
        String userId = "11";
        List<HistoricProcessInstance> processInstances = historyService
                .createHistoricProcessInstanceQuery()
                .processDefinitionId(processDefinitionId)
                .startedBy(userId)
                .includeProcessVariables()
                .list();
        List<EvectionVo> evectionVos = new ArrayList<>();
        for (HistoricProcessInstance processInstance : processInstances) {
            EvectionVo evectionVo = new EvectionVo();
            Map<String, Object> processVariables = processInstance.getProcessVariables();
            evectionVo
                    .setProcessInstanceId(processInstance.getId())
                    .setBeginDate((Date)processVariables.get("beginDate"))
                    .setEndDate((Date)processVariables.get("endDate"))
                    .setDescription((String) processVariables.get("description"))
                    .setNum((Integer)processVariables.get("num"))
                    .setApplicDate(processInstance.getStartTime());
            if(processInstance.getEndTime()!=null){//流程已结束
                evectionVo.setEnd(true);
                evectionVo.setApproved((Boolean)processVariables.get("approved"));
            }else{
                HistoricActivityInstance historicActivityInstance = historyService
                        .createHistoricActivityInstanceQuery()
                        .processInstanceId(processInstance.getId())
                        .unfinished()
                        .singleResult();
                //获取节点处理人信息
                evectionVo.setCurrentNode(historicActivityInstance.getActivityName());
//                Emp assigneeEmp = selectAssignEmp(historicActivityInstance.getAssignee());
//                evectionVo.setAssigneeEmp(assigneeEmp);
            }
            evectionVos.add(evectionVo);
        }
        out.println("出差流程详情==================");
        for (EvectionVo evectionVo : evectionVos) {
            out.println(evectionVo);
        }
    }

    /**
     * 查看我发起的流程详情
     */
    @Test
    public void queryEvectionApplyDetail() {
        String processInstanceId = "0c85bfba-df67-11ec-b68b-00ff37603673";
        List<HistoricActivityInstance> userTask = historyService
                .createHistoricActivityInstanceQuery()
                .processInstanceId(processInstanceId)
                .activityType("userTask")
                .list();
        out.println("活动节点===========================================");
        for (HistoricActivityInstance historicActivityInstance : userTask) {
            out.println("活动节点名称：" + historicActivityInstance.getActivityName());
            out.println("结束时间：" + historicActivityInstance.getEndTime());
            out.println("开始时间：" + historicActivityInstance.getStartTime());
            Long durationInMillis = historicActivityInstance.getDurationInMillis();
            out.println("持续时间：" + durationInMillis);
            out.println("节点处理人：" + historicActivityInstance.getAssignee());
            HistoricTaskInstance historicTaskInstance = historyService
                    .createHistoricTaskInstanceQuery()
                    .includeTaskLocalVariables()
                    .taskId(historicActivityInstance.getTaskId())
                    .singleResult();
            out.println("审批参数：" + historicTaskInstance.getTaskLocalVariables());
        }
    }

}
